﻿@page "/General/DrawOnCanvas"
@using LiveChartsCore.Generators
@using LiveChartsCore.Kernel
@using LiveChartsCore.Kernel.Sketches
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore;
@using LiveChartsCore.Drawing;
@using LiveChartsCore.SkiaSharpView;
@using LiveChartsCore.SkiaSharpView.Drawing;
@using LiveChartsCore.SkiaSharpView.Painting;
@using SkiaSharp;
@using ViewModelsSamples.General.DrawOnCanvas

<CartesianChart
    @ref="chart">
</CartesianChart>

@code {
    public CartesianChart chart = null!;
    private MotionGeometry _geometry = new MotionGeometry
    {
        Diameter = 20,
        Fill = new SolidColorPaint(SKColors.Blue.WithAlpha(100)),
        Stroke = new SolidColorPaint(SKColors.Blue, strokeWidth: 3)
    };
    private bool _isBig = false;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        chart.UpdateStarted += c =>
        {
            var locationInChartValues = new LvcPointD(5, 5);
            var locationInPixels = chart.ScaleDataToPixels(locationInChartValues);
            _geometry.X = (float)locationInPixels.X;
            _geometry.Y = (float)locationInPixels.Y;
            _geometry.Initialize(new(c));
        };

        chart.DataPointerDown += OnPointerDown;
    }

    private void OnPointerDown(IChartView chart, IEnumerable<ChartPoint> points)
    {
        _geometry.Diameter = _isBig ? 20 : 70;
        _isBig = !_isBig;
        chart.Invalidate();
    }
}
